package commands
{
	import com.bit101.components.PushButton;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class CommandExample extends Sprite
	{
		public function CommandExample()
		{
			var rectangle:Rectangle = new Rectangle(0xffffff,50);
			rectangle.x = 200;
			rectangle.y = 200;
			this.addChild(rectangle);
			
			var button:CommandButton = new CommandButton(this,0,250,"apply command");
			
			var container:CommandContainer = new CommandContainer(new RotateClockwiseCommand(rectangle),"rotate clockwise",0,0);
			this.addChild(container);
			
			container = new CommandContainer(new RotateCounterclockwiseCommand(rectangle),"rotate counter clockwise",0,55);
			this.addChild(container);
			
			container = new CommandContainer(new ScaleDownCommand(rectangle),"scale down",0,110);
			this.addChild(container);
			
			container = new CommandContainer(new ScaleUpCommand(rectangle),"scale up",0,165);
			this.addChild(container);
			
			var undoButton:PushButton = new PushButton(this,0,280,"undo",onUndo);
			
			var redoButton:PushButton = new PushButton(this,0,310,"redo",onRedo);
			
		}
		
		private function onUndo(event:MouseEvent):void{
			var stack:CommandStack = CommandStack.getInstance();
			if(stack.hasPreviousCommands()){
				var command:ICommand = stack.previous();
				if(command is IUndoableCommand){
					IUndoableCommand(command).undo();
				}
			}
		}
		
		private function onRedo(event:MouseEvent):void{
			var stack:CommandStack = CommandStack.getInstance();
			if(stack.hasPreviousCommands()){
				var command:ICommand = stack.next();
				if(command is IUndoableCommand){
					IRedoableCommand(command).redo();
				}
			}
		}
		
	}
}